﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace WebshopPizzaRio
{
    public class ShoppingCart : IEnumerable
    {
        private ArrayList shoppingcart;

        public ShoppingCart()
        {
            shoppingcart = new ArrayList();
        }

        public void AddItem(Items item)
        {
            int index = shoppingcart.IndexOf(item);
            if (index >= 0)
            {
                UpdateItem(item);
            }
            else
            {
                shoppingcart.Add(new Items(item.PizzaID, item.PizzaNaam, item.PrijsperStuk, item.Aantal));
            }
        }

        public Items getNewItem()
        {
            return new Items();
        }

        public void UpdateItem(Items item)
        {
            int index = shoppingcart.IndexOf(item);

            if (index >= 0)
            {
                Items currentItem = shoppingcart[index] as Items;
                currentItem.Aantal += item.Aantal;
                currentItem.PrijsperStuk = item.PrijsperStuk;
            }
            else
            {
                throw new ArgumentException(String.Format("Item = {0} is not in the shopping cart."));
            }
        }

        public void DeleteItem(Items item)
        {
            int index = shoppingcart.IndexOf(item);

            if (index >= 0)
            {
                shoppingcart.RemoveAt(index);
            }
            else
            {
                throw new ArgumentException(String.Format("Item = {0} is not in the shopping cart."));
            }
        }

        public decimal getTotaalBedrag()
        {
            decimal totaalBedrag = 0m;

            foreach (Items item in shoppingcart)
                totaalBedrag += item.Totaal;

            return totaalBedrag;
        }

        public ArrayList getShoppingCart
        {
            get { return shoppingcart; }
        }

        public IEnumerator GetEnumerator()
        {
            return (shoppingcart as IEnumerable).GetEnumerator();
        }
    }
}